package com.itzz.spring_zhoneHeng.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.ChaptersDao;
import com.itzz.spring_zhoneHeng.pojo.Chapters;
import com.itzz.spring_zhoneHeng.pojo.Classification;
import com.itzz.spring_zhoneHeng.service.ChaptersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ChaptersServiceImpl implements ChaptersService {
    @Autowired
    ChaptersDao chaptersDao;


    @Override
    //查询所有
    public RestResponse queryAll(Chapters chapters, Integer pageNum, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //查询所有
        List<Chapters> chaptersList = chaptersDao.queryAllChapters(chapters.getBookName());
        //添加到分页插件中
        PageInfo<Chapters> chaptersPageInfo = new PageInfo<>(chaptersList);
        return RestResponse.ok(chaptersPageInfo);
    }

    @Override
    //回显章节和书名及其审核状态
    public RestResponse queryChaptersById(Integer id) {
        Chapters chapters = chaptersDao.selectById(id);
        if(chapters==null){
            return RestResponse.fail(400,"章节不存在");
        }
        return RestResponse.ok(chapters);
    }

    @Override
    //修改书名和章节名及其状态
    public RestResponse update(Chapters chapters) {
        int i = chaptersDao.updateById(chapters);
        if(i==0){
            return RestResponse.fail(400,"修改失败");
        }
        return RestResponse.ok(i);
    }

    @Override
    @Transactional
    //添加章节
    public RestResponse add(Chapters chapters,Integer bookId) {
        chaptersDao.addChapters(chapters);
//        int i2 = chaptersDao.insert(chapters);
//        System.out.println(chapters.getId()+"/////////////////////////////////////////////////" +
//                "///////////////////////////////////////////////////////////////////////////");
        //添加bookid  添加到b_chap中  (@Param("bookId") Integer bookId,@Param("chapId") Integer chapId
        int i = chaptersDao.addb_chap(bookId, chapters.getId());



//
        if(i==0){
            return RestResponse.fail(400,"添加失败");
        }
        return RestResponse.ok(i);
    }
}
